<div class="contentArea">
    <div class="Item hr">
        <div class="current">解析文件</div>
    </div>
    <form>
        <table width="100%" border="0" cellspacing="0" cellpadding="0" class="table1">
            <tr id="upload_text">
                <th width="90">上传文件：</th>
                <td>
                    <input type="file" id="text" name="text" onchange='UploadImage(this);'>
                </td>
            </tr>


        </table>
    </form>
    <div class="commonBtnArea" style="display: none">
        <button class="btn submit">提交</button>
    </div>
</div>

<script type="text/javascript" src="__PUBLIC__/javascripts/fileupload/ajax_file_upload.js"></script>
<script type="text/javascript" src="__PUBLIC__/javascripts/fileupload/file_upload.js"></script>
<script type="text/javascript" src="__PUBLIC__/javascripts/admin/layer/layer.js"></script>


<script type="text/javascript">

    /**
     * 上传文件
     */
    function UploadImage(event) {
        var fileid = $(event).attr("id");
        var data = {'type':fileid};
        var url = "{:U('Novel/upload')}";

        uploadFile(fileid,data,url,function(data){

            if(data.code == 0){
                var filePath = data.data;
                $.post(
                    "Novel/novel_unzip",{filename:filePath},function(json){
                        $('#upload_text').hide();
                        var novel_list = json.novel_list;
                        var path_list = json.path_list;

                        var novel_list_html = '';
                        if (novel_list.length > 0) {
                            for (var i = 0; i < novel_list.length; i++) {
                                var item = novel_list[i];
                                novel_list_html += " <tr>" +
                                    "                <th width='100'>小说名称：</th>" +
                                    "                <td><input type='text' class='input' size='50' id='title_"+i+"' value='"+item['title']+"'></td>" ;
                                novel_list_html += " <th width='100'>作者：</th>" +
                                    "                <td><input type='text' class='input' size='50' id='author"+i+"' value='"+item['author']+"'></td>";
                                novel_list_html += " <td><td> <a onclick='text_info(\""+path_list[i]+"\")' href='javascript:void(0);'>详情</a> || <a id='add_sql' onclick='add_sql(\""+path_list[i]+"\")'  href='javascript:void(0);'>添加到数据库</a></td></td>" +
                                    "            </tr>";
                            }
                        }

                        $('.table1').append(novel_list_html);

                        //某小说文件直接添加到数据库
                        add_sql = function(path){
                            $.post(
                                "{:U('Novel/read_text')}",{filePath:path},function(data){
                                    if(data.code == 0){
                                        layer.msg(data.message,{icon:1,time: 1000});
                                    }
                                },'json'
                            )
                        }

                        //详情
                        text_info = function(path){
                            $('.commonBtnArea').show();
                            $.post(
                                "{:U('Novel/parse_text')}",{filePath:path},function(json){
                                    $('#upload_text').hide();
                                    var novel_info = json.data.novel_info;
                                    var chapter_info = json.data.chapter_data;

                                    var novel_html = "<tr>" +
                                        "                <th width='100'>小说名称：</th>" +
                                        "                <td><input type='text' class='input' size='50' name='title' value='"+novel_info['title']+"'></td>" +
                                        "            </tr>" +
                                        " <tr>" +
                                        "                <th width='100'>小说作者：</th>" +
                                        "                <td><input type='text' class='input' size='50' name='author' value='"+novel_info['author']+"'></td>" +
                                        "            </tr>" +
                                        " <tr>" +
                                        "                <th width='100'>小说字数：</th>" +
                                        "                <td><input type='text' class='input' size='50' name='words_number' value='"+novel_info['words_number']+"'></td>" +
                                        "            </tr>" +
                                        " <tr>" +
                                        "                <th width='100'>来源名称：</th>" +
                                        "                <td><input type='text' class='input' size='50' name='source_addr' value='"+novel_info['source_addr']+"'></td>" +
                                        "            </tr>" +
                                        " <tr>" +
                                        "                <th width='100'>来源网址：</th>" +
                                        "                <td><input type='text' class='input' size='50' name='source_url' value='"+novel_info['source_url']+"'></td>" +
                                        "            </tr>" +
                                        " <tr>" +
                                        "                <th width='100'>小说简介：</th>" +
                                        "                <td><textarea type='text' class='jq_watermark' style='width: 400px;height: 80px;' name='intro'>"+novel_info['intro']+"</textarea></td>" +
                                        "            </tr>" +
                                        " <tr>" ;

                                    var chapter_html = '';
                                    if (chapter_info.length > 0) {
                                        for (var i = 0; i < chapter_info.length; i++) {
                                            var item = chapter_info[i];
                                            chapter_html += " <tr>" +
                                                "                <th width='100'>章节名称：</th>" +
                                                "                <td><input type='text' class='input' size='50' id='title_"+i+"' value='"+item['title']+"'></td>" +
                                                "            </tr>" ;
                                            chapter_html += " <tr>" +
                                                "                <th width='100'>字数：</th>" +
                                                "                <td><input type='text' class='input' size='50' id='words_number_"+i+"' value='"+item['words_number']+"'></td>" +
                                                "            </tr>";
                                            chapter_html += " <tr>" +
                                                "                <th width='100'>章节简介：</th>" +
                                                "                <td><textarea type='text' class='jq_watermark' id='content_"+i+"' style='width: 400px;height: 80px;'>"+item['content']+"</textarea></td>" +
                                                "            </tr>";
                                        }
                                    }

                                    $('.table1').append(novel_html+chapter_html);

                                    $(".submit").click(function(){
                                        //小说信息
                                        var novel_data = novel_info;
                                        novel_data.title = $('input[name=title]').val();
                                        novel_data.author = $('input[name=author]').val();
                                        novel_data.words_number = $('input[name=words_number]').val();
                                        novel_data.source_addr = $('input[name=source_addr]').val();
                                        novel_data.source_url = $('input[name=source_url]').val();
                                        novel_data.intro = $('textarea[name=intro]').val();

                                        //章节信息
                                        var chapter_data = [];
                                        for(var i = 0; i < chapter_info.length; i++) {
                                            var chapter_item = chapter_info[i];
                                            chapter_item.title = $("#title_"+i).val();
                                            chapter_item.words_number = $("#words_number_"+i).val();
                                            chapter_item.content = $("#content_"+i).val();
                                            chapter_data.push(chapter_item);
                                        }

                                        console.log(novel_data,chapter_data);
                                        $.post("{:U('Novel/add_novel')}",{novel_data:novel_data,chapter_data:chapter_data},function(data){
                                            if(data.code == 0){
                                                layer.msg(data.message,{icon:1,time: 1000},function(){location.href="Novel/index"});
                                            }
                                        },'json')
                                    });

                                },'json'
                            )
                        }

                    },'json'
                )
            }
        }, 'json');
    }

</script>


